package com.gzy.cbrsystem.core.insomnia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;



import com.gzy.cbrsystem.core.interfaces.Case;
import com.gzy.cbrsystem.core.interfaces.CaseBasic;
import com.gzy.cbrsystem.insomnia.ClinicalBit;
import com.gzy.cbrsystem.insomnia.ClinicalBitDAO;
import java.util.ArrayList;
public class InsomniaCaseBase {
	protected ClinicalBitDAO clinicalBitDAO;
	protected List<Case> cases;
	public InsomniaCaseBase()
	{
		//this.initCaseBase();
	}
	public ClinicalBitDAO getClinicalBitDAO() {
		return clinicalBitDAO;
	}
	public void setClinicalBitDAO(ClinicalBitDAO clinicalBitDAO) {
		this.clinicalBitDAO = clinicalBitDAO;
	}
	public List<Case> getCases() {
		return cases;
	}
	public void setCases(List<Case> cases) {
		this.cases = cases;
	}
	public List<Case> reConstructCaseBase()
	{
		List<Case> cases = new ArrayList<Case>();
		
		try 
		{			
			boolean solutionExist=false;
			List descriptions = this.clinicalBitDAO.findAll();
			HashMap<Object, CaseBasic> solutions =new HashMap<Object, CaseBasic>();
			HashMap<Object, CaseBasic> rearrangements = null;
			HashMap<Object, CaseBasic> results = null;
			
			if(solutionExist )
			{
				//TODO: add solution Database
				List sol = new ArrayList();
				
				for(Iterator iter = sol.iterator(); iter.hasNext();)
				{
					//TODO: transform CaseBasic!
					CaseBasic ci = (CaseBasic)iter.next();
					solutions.put(ci.getCaseIdentifier().get(ci), ci);
				}
			}
			
			for(Iterator iter = descriptions.iterator(); iter.hasNext();)
			{
				Case ic = new InsomniaCase();
				ClinicalBit cb=(ClinicalBit)iter.next();
				CaseBasic desc = new InsomniaCaseDescription(cb);
				ic.setCaseDescription(desc);
				
				if(solutionExist)
				{
					//TODO: transform!!!
					CaseBasic cc = solutions.get(desc.getCaseIdentifier().get(desc));
					if(cc != null)
						ic.setSolution(cc);
				}		
				cases.add(ic);
				
			}		
			
			
			
			//transaction.commit();
			//session.close();
			
		} catch (Exception e) {
			System.out.println("CaseBase reconstruction error!");
		}
		return cases;
	}
	public List<Case> initFromFile()
	{
		return this.reConstructCaseBase();
	}
	public void initCaseBase()
	{
		if(this.cases==null)
		this.cases=this.initFromFile();
	}
}
